package com.management.department.teacherinfosystem.config;

import com.management.department.teacherinfosystem.Entity.*;
import com.management.department.teacherinfosystem.Repository.ActivityRepository;
import com.management.department.teacherinfosystem.Repository.ActivityAttachmentRepository;
import com.management.department.teacherinfosystem.Repository.UserRepository;
import com.management.department.teacherinfosystem.Repository.RoleRepository;
import com.management.department.teacherinfosystem.Service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Component
@RequiredArgsConstructor
public class ActivityDataInitializer implements ApplicationRunner {

    private final ActivityRepository activityRepository;
    private final ActivityAttachmentRepository attachmentRepository;
    private final UserRepository userRepository;
    private final RoleRepository roleRepository;
    private final UserService userService;

    @Override
    @Transactional
    public void run(ApplicationArguments args) throws Exception {
        if (activityRepository.count() > 0) {
            return;
        }

        // 确保 teacher 角色存在
        roleRepository.findByName("teacher").orElseGet(() -> {
            Role r = new Role();
            r.setName("teacher");
            r.setDescription("教职工");
            return roleRepository.save(r);
        });

        // 确保示例用户存在
        User teacher = userRepository.findByUsername("t001").orElse(null);
        if (teacher == null) {
            teacher = userService.createUser("t001", "123456", "张三", "信息管理系", "teacher");
        }

        // 活动 1：学术会议，含报告
        Activity a1 = new Activity();
        a1.setActivityName("全国教育信息化年会");
        a1.setActivityType("学术会议");
        a1.setOrganizer("中国教育学会");
        a1.setOrganizerLevel("国家级");
        a1.setLocation("线下·北京会议中心");
        a1.setStartDate(LocalDate.now().minusDays(30));
        a1.setEndDate(LocalDate.now().minusDays(28));
        a1.setParticipantName(teacher.getName());
        a1.setParticipantRole("报告人");
        a1.setHasReport(true);
        a1.setReportTitle("智慧教育平台建设与应用");
        a1.setUser(teacher);
        a1.setStatus("APPROVED");
        a1.setReviewComment("内容翔实，材料完整");
        a1.setReviewTime(LocalDateTime.now().minusDays(25));
        a1.setCreatedAt(LocalDateTime.now().minusDays(31));
        a1.setUpdatedAt(LocalDateTime.now().minusDays(25));
        Activity savedA1 = activityRepository.save(a1);

        ActivityAttachment att11 = new ActivityAttachment();
        att11.setActivity(savedA1);
        att11.setFileName("会议通知.pdf");
        att11.setFilePath("uploads/activities/notice1.pdf");
        att11.setFileType("会议通知");
        att11.setFileSize(256000L);
        att11.setContentType("application/pdf");
        attachmentRepository.save(att11);

        ActivityAttachment att12 = new ActivityAttachment();
        att12.setActivity(savedA1);
        att12.setFileName("参会证明.jpg");
        att12.setFilePath("uploads/activities/proof1.jpg");
        att12.setFileType("参会证明");
        att12.setFileSize(180000L);
        att12.setContentType("image/jpeg");
        attachmentRepository.save(att12);

        ActivityAttachment att13 = new ActivityAttachment();
        att13.setActivity(savedA1);
        att13.setFileName("报告PPT.pptx");
        att13.setFilePath("uploads/activities/report1.pptx");
        att13.setFileType("报告PPT");
        att13.setFileSize(2048000L);
        att13.setContentType("application/vnd.openxmlformats-officedocument.presentationml.presentation");
        attachmentRepository.save(att13);

        // 活动 2：教学研讨会，无报告
        Activity a2 = new Activity();
        a2.setActivityName("省级教学研讨会");
        a2.setActivityType("教学研讨会");
        a2.setOrganizer("省教育厅");
        a2.setOrganizerLevel("省部级");
        a2.setLocation("线上·腾讯会议");
        a2.setStartDate(LocalDate.now().minusDays(14));
        a2.setEndDate(LocalDate.now().minusDays(13));
        a2.setParticipantName(teacher.getName());
        a2.setParticipantRole("参会代表");
        a2.setHasReport(false);
        a2.setUser(teacher);
        a2.setStatus("PENDING");
        a2.setCreatedAt(LocalDateTime.now().minusDays(15));
        a2.setUpdatedAt(LocalDateTime.now().minusDays(15));
        Activity savedA2 = activityRepository.save(a2);

        ActivityAttachment att21 = new ActivityAttachment();
        att21.setActivity(savedA2);
        att21.setFileName("邀请函.pdf");
        att21.setFilePath("uploads/activities/invite2.pdf");
        att21.setFileType("邀请函");
        att21.setFileSize(300000L);
        att21.setContentType("application/pdf");
        attachmentRepository.save(att21);

        // 活动 3：竞赛评审
        Activity a3 = new Activity();
        a3.setActivityName("全国大学生创新竞赛评审");
        a3.setActivityType("竞赛评审");
        a3.setOrganizer("教育部高等教育司");
        a3.setOrganizerLevel("国家级");
        a3.setLocation("线下·上海某高校");
        a3.setStartDate(LocalDate.now().minusDays(7));
        a3.setEndDate(LocalDate.now().minusDays(6));
        a3.setParticipantName(teacher.getName());
        a3.setParticipantRole("评委");
        a3.setHasReport(false);
        a3.setUser(teacher);
        a3.setStatus("APPROVED");
        a3.setReviewComment("资质符合");
        a3.setReviewTime(LocalDateTime.now().minusDays(5));
        a3.setCreatedAt(LocalDateTime.now().minusDays(8));
        a3.setUpdatedAt(LocalDateTime.now().minusDays(5));
        activityRepository.save(a3);
    }
} 